package com.efounder.frame.activity;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.core.xml.StubObject;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.frame.baseui.EFActivity;
import com.efounder.mobilemanager.R;
import com.efounder.util.AbFragmentManager;

import java.util.Hashtable;


/**
 * 通用的跳转Fragment Activity
 * 1.需要传入Fragment全名
 * 2.会把Activity的bundle传递到Fragment中
 *
 * @author hudq
 */

public class CommonTransformFragmentActivity extends EFActivity {

    public static final String EXTRA_FRAGMENT_NAME = "fragmentName";
    /**
     * value:String
     **/
    public static final String EXTRA_TITLE_NAME = "titleName";
    /**
     * value:int(例如：View.VISIBLE)
     **/
    public static final String EXTRA_TITLE_LEFT_VISIBILITY = "left_visibility";
    /**
     * value:int(例如：View.VISIBLE)
     **/
    public static final String EXTRA_TITLE_RIGHT_VISIBILITY = "right_visibility";
    /**
     * value:boolean
     **/
    public static final String EXTRA_HIDE_TITLE_BAR = "hide_title_bar";

    public static final String EXTRA_THEME = "theme";

    private String fragmentName;
    private Fragment fragment;
    private StubObject stubObject;
    private AbFragmentManager abFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ef_activity_transform_fragment);
        stubObject = (StubObject) getIntent().getExtras().getSerializable("stubObject");
        initTitleView();
        fragmentName = getIntent().getStringExtra(EXTRA_FRAGMENT_NAME);
        abFragmentManager = new AbFragmentManager(this);


        try {
            BaseFragment fragment = abFragmentManager.getFragment(stubObject);
            fragment.setAlone(true);
            //   fragment.setArguments(getIntent().getExtras());
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, fragment);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("WrongConstant")
    private void initTitleView() {
        //中-标题
        if (getIntent().hasExtra(EXTRA_TITLE_NAME)) {
            TextView titleTextView = (TextView) getEFTitleView().getCenterView();
            titleTextView.setText(getIntent().getStringExtra(EXTRA_TITLE_NAME));
        } else {
            if (stubObject != null) {
                Hashtable<String, String> hashtable = stubObject.getStubTable();
                //String title = hashtable.get("title");
                String title = null;
                if (hashtable.containsKey("systemName")) {
                    title = hashtable.get("systemName");
                }
                if (title == null && hashtable.containsKey("caption")) {
                    title = hashtable.get("caption");
                }


                TextView titleTextView = (TextView) getEFTitleView().getCenterView();
                titleTextView.setText(title);

            }

        }
        //左-返回
        if (getIntent().hasExtra(EXTRA_TITLE_LEFT_VISIBILITY)) {
            int visibility = getIntent().getIntExtra(EXTRA_TITLE_LEFT_VISIBILITY, View.VISIBLE);
            getEFTitleView().getLeftImageView().setVisibility(visibility);
        }
        //右-菜单
        if (getIntent().hasExtra(EXTRA_TITLE_RIGHT_VISIBILITY)) {
            int visibility = getIntent().getIntExtra(EXTRA_TITLE_RIGHT_VISIBILITY, View.VISIBLE);
            getEFTitleView().getRightImageView().setVisibility(visibility);
        } else {
            getEFTitleView().getRightImageView().setVisibility(View.INVISIBLE);

        }
        //隐藏TitleBar
        //有的Fragment需要使用标题栏的按钮进行跳转，在此隐藏掉此Activity的标题栏
        if (getIntent().hasExtra(EXTRA_HIDE_TITLE_BAR) && getIntent().getBooleanExtra(EXTRA_HIDE_TITLE_BAR, false)) {
            getEFTitleView().setVisibility(View.VISIBLE);
        } else {
            getEFTitleView().setVisibility(View.GONE);
        }

    }


    public void back(View view) {

        finish();
    }

    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && fragment instanceof OnKeyBackListener) {
            OnKeyBackListener onKeyBackListener = (OnKeyBackListener) fragment;
            boolean forbidPropagate = onKeyBackListener.OnKeyBack(keyCode, event);
            if (forbidPropagate) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 点击返回按钮的监听
     *
     * @author hudq
     */
    public interface OnKeyBackListener {
        /**
         * 点击返回
         *
         * @param keyCode KeyEvent.KEYCODE_BACK
         * @param event
         * @return 返回true, 防止事件进一步传播, 否则，表明你没有处理这个事件,它会继续传播。
         */
        public boolean OnKeyBack(int keyCode, KeyEvent event);
    }

}
